package com.demo.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demo.entity.Technology;
import com.demo.entity.TreeNode;

import net.sf.json.JSONArray;

@SuppressWarnings("unchecked")
public class TreeAction{

	//根据ID查询子节点
	public void getTreeData(HttpServletRequest request, HttpServletResponse response){
		try{
			String id = request.getParameter("id");
			//System.out.println("父节点ID：" + id);
			
			List<Technology> list = TreeData();
			List<TreeNode> tn = new ArrayList<TreeNode>();
			if(id == null || id.equals("root")){
				for(Technology t : list){
					if(t.getPid().equals("0")){
						TreeNode treeNode = new TreeNode();
						treeNode.setId(t.getId());
						treeNode.setText(t.getName());
						treeNode.setLeaf(false);
						tn.add(treeNode);
					}
				}
			}else{
				for(Technology t : list){
					if(t.getPid().equals(id)){
						TreeNode treeNode = new TreeNode();
						treeNode.setId(t.getId());
						treeNode.setText(t.getName());
						tn.add(treeNode);
						for(Technology te : list){
							if(t.getId().equals(te.getPid())){
								treeNode.setLeaf(false);
							}
						}
					}
				}
			}

			JSONArray ja = JSONArray.fromObject(tn);
			//System.out.println(ja);
			PrintWriter out = response.getWriter();
			out.print(ja.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public List<Technology> TreeData(){
		List<Technology> list = new ArrayList<Technology>();
		Technology t1 = new Technology();
		t1.setId("1");
		t1.setName("前端");
		t1.setPid("0");
		list.add(t1);

		Technology t2 = new Technology();
		t2.setId("2");
		t2.setName("后端");
		t2.setPid("0");
		list.add(t2);

		Technology t3 = new Technology();
		t3.setId("3");
		t3.setName("C");
		t3.setPid("2");
		list.add(t3);

		Technology t4 = new Technology();
		t4.setId("4");
		t4.setName("C#");
		t4.setPid("2");
		list.add(t4);

		Technology t5 = new Technology();
		t5.setId("5");
		t5.setName("Java");
		t5.setPid("2");
		list.add(t5);

		Technology t6 = new Technology();
		t6.setId("6");
		t6.setName("PHP");
		t6.setPid("2");
		list.add(t6);

		Technology t7 = new Technology();
		t7.setId("7");
		t7.setName("Servlet");
		t7.setPid("5");
		list.add(t7);

		Technology t8 = new Technology();
		t8.setId("8");
		t8.setName("JSP");
		t8.setPid("5");
		list.add(t8);

		Technology t9 = new Technology();
		t9.setId("9");
		t9.setName("Struts");
		t9.setPid("5");
		list.add(t9);

		Technology t10 = new Technology();
		t10.setId("10");
		t10.setName("Spring");
		t10.setPid("5");
		list.add(t10);

		Technology t11 = new Technology();
		t11.setId("11");
		t11.setName("Hibernate");
		t11.setPid("5");
		list.add(t11);

		Technology t12 = new Technology();
		t12.setId("12");
		t12.setName("HTML");
		t12.setPid("1");
		list.add(t12);

		Technology t13 = new Technology();
		t13.setId("13");
		t13.setName("JavaScript");
		t13.setPid("1");
		list.add(t13);

		Technology t14 = new Technology();
		t14.setId("14");
		t14.setName("CSS");
		t14.setPid("1");
		list.add(t14);
		
		Technology t15 = new Technology();
		t15.setId("15");
		t15.setName("Session");
		t15.setPid("11");
		list.add(t15);
		
		Technology t16 = new Technology();
		t16.setId("16");
		t16.setName("SessionFactory");
		t16.setPid("11");
		list.add(t16);
		
		Technology t17 = new Technology();
		t17.setId("17");
		t17.setName("Transaction");
		t17.setPid("11");
		list.add(t17);
		
		Technology t18 = new Technology();
		t18.setId("18");
		t18.setName("Query");
		t18.setPid("11");
		list.add(t18);
		
		Technology t19 = new Technology();
		t19.setId("19");
		t19.setName("Criteria");
		t19.setPid("11");
		list.add(t19);
		
		Technology t20 = new Technology();
		t20.setId("20");
		t20.setName("Configuration");
		t20.setPid("11");
		list.add(t20);
		
		return list;
	}

}